
#include "Receivers.h"
#include "LPC23xx.h"
#include "modem.h"
#include "macros.h"
#include "io.h"
#include "USART.h"
#include "string.h"
#include "debug.h"


unsigned char TE_DirectModemMode = 0;

static unsigned int pModemMessage;
static char ModemMessage[MODEM_MESSGAGE_SIZE];
char ModemBuffer[MODEM_BUFFER_SIZE];
//char ModemMessage[MODEM_MESSGAGE_SIZE];
char *Modem_Message;


static unsigned int pAuxPortBuffer;
char AuxPort_Buffer[AUX_PORT_MESSAGE_SIZE];
char *AuxPort_Message;

int AUX_PORT_REC_COMPLETE;

static int modem_buf_clear;

void modem_clear_buffer ( void )
{
	modem_buf_clear = 1;
}


void receivers_init ( void )
{
	modem_buf_clear = 0;
	MemSet((unsigned char*)ModemBuffer, MODEM_BUFFER_SIZE, 0);
	MODEM_RTS = 0;
}


void Aux_Port_Receiver ( void )
{
	unsigned char Buffer, id, EndOfString;

	while ( ((id = U0IIR) & 0x01) == 0 )
	{
		if ( (id & 0x0E) == 0x04 )
		{
			Buffer = U0RBR;

			if ( TE_DirectModemMode )
			{
				EndOfString = '\n';
			}
			else
			{
				EndOfString = '\r';
			}

			if ( Buffer == EndOfString )  
			{
				AuxPort_Buffer[pAuxPortBuffer] = 0;
				pAuxPortBuffer = 0;
				AUX_PORT_REC_COMPLETE = TRUE;
			}
			else
			{
				AuxPort_Buffer[pAuxPortBuffer] = Buffer;
				if ( ++pAuxPortBuffer >= AUX_PORT_REC_BUFFER_SIZE )
					pAuxPortBuffer = 0;
			}

		}
		else
		{
			U0LSR;
			U0RBR;
		}
	}

	VICVectAddr = 0;             // clear this interrupt from the VIC
}

void Modem_Receiver ( void )
{
	unsigned char id;

	while ( ((id = U2IIR) & 0x01) == 0 )
	{
		if ( (id & 0x0E) == 0x04 )
		{
			ModemMessage[pModemMessage] = U2RBR;
			
			PUTCHAR(ModemMessage[pModemMessage]);

			if ( ModemMessage[pModemMessage] == 0 )
			{
				pModemMessage = 0;
			}
			else
				// monitor for "<CR><LF>"
				if ( pModemMessage >= 2 && ModemMessage[pModemMessage] == '\n'  && ModemMessage[pModemMessage-1] == '\r')// && 
					  //ModemMessage[pModemMessage-2] == 'K' && ModemMessage[pModemMessage-3] == 'O' )
				{
/* 					if ( pModemMessage == 2 ) */
/* 					{ */
/* 						pModemMessage = 0;  */
/* 					} */
/* 					else */
/* 					{ */
						ModemMessage[pModemMessage+1] = 0;	/* end of string */
						pModemMessage = 0;
						/* Stop receive */
						MODEM_RTS = 1;
//					}
				}
				else
					if ( ++pModemMessage >= MODEM_MESSGAGE_SIZE ) //MODEM_BUFFER_SIZE )
					{
						pModemMessage = 0;
					}
			
		}
		else
		{
			U2LSR;
			U2RBR;
		}
	}
	
	VICVectAddr = 0;             // clear this interrupt from the VIC
}


void Drive_Receivers ( void )
{
	/* Aux Port */
	if ( AuxPort_Message )
		AuxPort_Message = NULL;
	
	if ( AUX_PORT_REC_COMPLETE )
	{
		AUX_PORT_REC_COMPLETE = FALSE;
		AuxPort_Message = AuxPort_Buffer;
	}

	/* Modem Port */
	if ( Modem_Message )
	{
		if ( modem_buf_clear || StrStr( ModemBuffer, "\r\nOK\r\n" ) )
		{
			modem_buf_clear = 0;
			//DBG("\n%s;\n ===== BUFFER =========\n[%s]\n=====================\n",__FUNCTION__,ModemBuffer); 
			MemSet((unsigned char*)ModemBuffer, MODEM_BUFFER_SIZE, 0);
		}

		Modem_Message = NULL; 
		MODEM_RTS = 0;				  /* continue receive */
	}

	if ( MODEM_RTS )
	{
		Modem_Message = ModemMessage;
		StrCat(ModemBuffer, ModemMessage);
		//DBG("\n<%s>\n",Modem_Message); 
	}
} 















/*
Drive_Receivers;
 ===== BUFFER =========
[
+CMGL: 1,"REC READ","+972526144338",,"12/02/26,14:20:48+08"\r\n
Gjtwmgj .a.g.\r\n
+CMGL: 2,"REC READ","+972526144338",,"12/02/26,14:21:30+08"
G+tjm+Pj.-ujr
+CMGL: 3,"REC READ","+972526144338",,"12/02/26,14:25:16+08"
Ptjm.,

OK
]
=====================

14:35:50.258> 
 MainLoop: 17 uSec

14:36:40.258> 
 MainLoop: 17 uSec

14:36:42.993> 
SEND 2 AT+CMGL="REC READ"\0D

14:36:43.040> 
0A 63 6D 64 5F 70 72 6F   |  .cmd_pro
63 65 73 73 6F 72 3B 20   |  cessor; 
69 6E 5F 64 65 76 3A 20   |  in_dev: 
31 3B 20 63 6D 64 3A 20   |  1; cmd: 
53 45 4E 44 20 32 20 41   |  SEND 2 A
54 2B 43 4D 47 4C 3D 22   |  T+CMGL="
52 45 43 20 52 45 41 44   |  REC READ
22 3B 20 65 72 72 6F 72   |  "; error
3A 20 30 3B 0A 0D 4E 58   |  : 0;..NX
0D                        |  .

14:36:43.133> 
0A 44 72 69 76 65 5F 52   |  .Drive_R
65 63 65 69 76 65 72 73   |  eceivers
3B 20 3C 20 47 0A 2B 43   |  ; < G.+C
4D 47 4C 3A 20 31 2C 22   |  MGL: 1,"
52 45 43 20 52 45 41 44   |  REC READ
22 2C 22 2B 39 37 32 35   |  ","+9725
32 36 31 34 34 33 33 38   |  26144338
22 2C 2C 22 31 32 2F 30   |  ",,"12/0
32 2F 32 36 2C 31 34 3A   |  2/26,14:
32 30 3A 34 38 2B 30 38   |  20:48+08
22 0D 0A 20 3E 0A 0A 44   |  ".. >..D
72 69 76 65 5F 52 65 63   |  rive_Rec
65 69 76 65 72 73 3B 20   |  eivers; 
3C 20 47 6A 74 77 6D 67   |  < Gjtwmg
6A 20 2E 61 2E 67 2E 0D   |  j .a.g..
0A 20 3E 0A               |  . >.

14:36:43.211> 
0A 44 72 69 76 65 5F 52   |  .Drive_R
65 63 65 69 76 65 72 73   |  eceivers
3B 20 3C 20 47 43 4D 47   |  ; < GCMG
4C 3A 20 32 2C 22 52 45   |  L: 2,"RE
43 20 52 45 41 44 22 2C   |  C READ",
22 2B 39 37 32 35 32 36   |  "+972526
31 34 34 33 33 38 22 2C   |  144338",
2C 22 31 32 2F 30 32 2F   |  ,"12/02/
32 36 2C 31 34 3A 32 31   |  26,14:21
3A 33 30 2B 30 38 22 0D   |  :30+08".
0A 20 3E 0A 0A 44 72 69   |  . >..Dri
76 65 5F 52 65 63 65 69   |  ve_Recei
76 65 72 73 3B 20 3C 20   |  vers; < 
47 2B 74 6A 6D 2B 50 6A   |  G+tjm+Pj
2E 2D 75 6A 72 0D 0A 20   |  .-ujr.. 
3E 0A                     |  >.

14:36:43.290> 
0A 44 72 69 76 65 5F 52   |  .Drive_R
65 63 65 69 76 65 72 73   |  eceivers
3B 20 3C 20 50 43 4D 47   |  ; < PCMG
4C 3A 20 33 2C 22 52 45   |  L: 3,"RE
43 20 52 45 41 44 22 2C   |  C READ",
22 2B 39 37 32 35 32 36   |  "+972526
31 34 34 33 33 38 22 2C   |  144338",
2C 22 31 32 2F 30 32 2F   |  ,"12/02/
32 36 2C 31 34 3A 32 35   |  26,14:25
3A 31 36 2B 30 38 22 0D   |  :16+08".
0A 20 3E 0A 0A 44 72 69   |  . >..Dri
76 65 5F 52 65 63 65 69   |  ve_Recei
76 65 72 73 3B 20 3C 20   |  vers; < 
50 74 6A 6D 2E 2C 0D 0A   |  Ptjm.,..
20 3E 0A 0A 44 72 69 76   |   >..Driv
65 5F 52 65 63 65 69 76   |  e_Receiv
65 72 73 3B 20 3C 20 0D   |  ers; < .
0A 4F 4B 0D 0A 20 3E 0A   |  .OK.. >.
0A 44 72 69 76 65 5F 52   |  .Drive_R
65 63 65 69 76 65 72 73   |  eceivers
3B 0A 20 3D 3D 3D 3D 3D   |  ;. =====
20 42 55 46 46 45 52 20   |   BUFFER 
3D 3D 3D 3D 3D 3D 3D 3D   |  ========
3D 0A 5B 0D 0A 2B 43 4D   |  =.[..+CM
47 4C 3A 20 31 2C 22 52   |  GL: 1,"R
45 43 20 52 45 41 44 22   |  EC READ"
2C 22 2B 39 37 32 35 32   |  ,"+97252
36 31 34 34 33 33 38 22   |  6144338"
2C 2C 22 31 32 2F 30 32   |  ,,"12/02
2F 32 36 2C 31 34 3A 32   |  /26,14:2
30 3A 34 38 2B 30 38 22   |  0:48+08"
0D 0A 47 6A 74 77 6D 67   |  ..Gjtwmg
6A 20 2E 61 2E 67 2E 0D   |  j .a.g..
0A 2B 43 4D 47 4C 3A 20   |  .+CMGL: 
32 2C 22 52 45 43 20 52   |  2,"REC R
45 41 44 22 2C 22 2B 39   |  EAD","+9
37 32 35 32 36 31 34 34   |  72526144
33 33 38 22 2C 2C 22 31   |  338",,"1
32 2F 30 32 2F 32 36 2C   |  2/02/26,
31 34 3A 32 31 3A 33 30   |  14:21:30
2B 30 38 22 0D 0A 47 2B   |  +08"..G+
74 6A 6D 2B 50 6A 2E 2D   |  tjm+Pj.-
75 6A 72 0D 0A 2B 43 4D   |  ujr..+CM
47 4C 3A 20 33 2C 22 52   |  GL: 3,"R
45 43 20 52 45 41 44 22   |  EC READ"
2C 22 2B 39 37 32 35 32   |  ,"+97252
36 31 34 34 33 33 38 22   |  6144338"
2C 2C 22 31 32 2F 30 32   |  ,,"12/02
2F 32 36 2C 31 34 3A 32   |  /26,14:2
35 3A 31 36 2B 30 38 22   |  5:16+08"
0D 0A 50 74 6A 6D 2E 2C   |  ..Ptjm.,
0D 0A 0D 0A 4F 4B 0D 0A   |  ....OK..
5D 0A 3D 3D 3D 3D 3D 3D   |  ].======
3D 3D 3D 3D 3D 3D 3D 3D   |  ========
3D 3D 3D 3D 3D 3D 3D 0A   |  =======.
*/
